package god530.com.simpleapp_slide.account.title.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 创建创建数据库等
 * Created by WhatINeed on 16-1-30.
 */
public class DBBase extends SQLiteOpenHelper{
/*
    public DBBase(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }*/

    public DBBase(Context context/*, String name, SQLiteDatabase.CursorFactory factory, int version*/) {
        super(context, DBConstants.DB_NAME, null, DBConstants.vesion); // 通过DBConstants类中修改vesion版本号
    }

    public SQLiteDatabase db ;



    //    CREATE TABLE account_type (id INTEGER PRIMARY KEY AUTOINCREMENT,key, value, MONEY);
//    CREATE TABLE borrow (id INTEGER PRIMARY KEY AUTOINCREMENT, uri, MONEY, type_detail, account_detail,  member, date, remind_model, remind_date, interest, remarks, time_span,update_time);
//    CREATE TABLE borrow_member (id INTEGER PRIMARY KEY AUTOINCREMENT,name, MONEY);
//    CREATE TABLE budget (id INTEGER PRIMARY KEY AUTOINCREMENT,type, total, expenses);
//    CREATE TABLE delete_item (id INTEGER PRIMARY KEY AUTOINCREMENT,time_span);
//    CREATE TABLE expenses (id INTEGER PRIMARY KEY AUTOINCREMENT, uri, MONEY, type_main, type_detail, account_detail, date, member, remarks, time_span,update_time);
//    CREATE TABLE expenses_left (id INTEGER PRIMARY KEY AUTOINCREMENT,left);
//    CREATE TABLE expenses_right (id INTEGER PRIMARY KEY AUTOINCREMENT,key, value, usually_key);
//    CREATE TABLE income_type (id INTEGER PRIMARY KEY AUTOINCREMENT,income_type);
//    CREATE TABLE income (id INTEGER PRIMARY KEY AUTOINCREMENT, uri, MONEY, type_detail,account_detail, date, member,remarks, time_span,update_time);
//    CREATE TABLE member (id INTEGER PRIMARY KEY AUTOINCREMENT,member);
//    CREATE TABLE remind (id INTEGER PRIMARY KEY AUTOINCREMENT,isBorrow, person, MONEY, time_remind, timeSpanOfSequence);

//    CREATE TABLE time_sequence (id INTEGER PRIMARY KEY AUTOINCREMENT,time_span_four, account_type, date, time_span);
//    CREATE TABLE transfer (id INTEGER PRIMARY KEY AUTOINCREMENT, uri, MONEY, type_detail,transfer_account_from, transfer_account_to, date, member, remarks,time_span,update_time);
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建数据库
        db.execSQL(DBConstants.CREATE_ACCOUNTTYPE_SQL);
        db.execSQL(DBConstants.CREATE_BORROW_SQL);
        db.execSQL(DBConstants.CREATE_BORROWNUMBER_SQL);
        db.execSQL(DBConstants.CREATE_BUDGET_SQL);
        db.execSQL(DBConstants.CREATE_DELETEITEM_SQL);
        db.execSQL(DBConstants.CREATE_EXPENSES_SQL);
        db.execSQL(DBConstants.CREATE_EXPENSELEFT_SQL);
        db.execSQL(DBConstants.CREATE_EXPENSESRIGHT_SQL);
        db.execSQL(DBConstants.CREATE_INCOMETYPE_SQL);
        db.execSQL(DBConstants.CREATE_INCOME_SQL);
        db.execSQL(DBConstants.CREATE_MEMBER_SQL);
        db.execSQL(DBConstants.CREATE_REMIND_SQL);
        db.execSQL(DBConstants.CREATE_TIMESEQUENCE_SQL);
        db.execSQL(DBConstants.CREATE_TRANSFER_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 更新数据库
        if(newVersion > oldVersion) { // 先删除数据库， 然后再创建数据库
            db.execSQL(DBConstants.DROP_ACCOUNTTYPE);
            db.execSQL(DBConstants.CREATE_ACCOUNTTYPE_SQL);

            db.execSQL(DBConstants.DROP_BORROW);
            db.execSQL(DBConstants.CREATE_BORROW_SQL);

            db.execSQL(DBConstants.DROP_BORROW_MEMBER);
            db.execSQL(DBConstants.CREATE_BORROWNUMBER_SQL);

            db.execSQL(DBConstants.DROP_BUDGET);
            db.execSQL(DBConstants.CREATE_BUDGET_SQL);

            db.execSQL(DBConstants.DROP_DELETE_ITEM);
            db.execSQL(DBConstants.CREATE_DELETEITEM_SQL);

            db.execSQL(DBConstants.DROP_EXPENSES);
            db.execSQL(DBConstants.CREATE_EXPENSES_SQL);

            db.execSQL(DBConstants.DROP_EXPENSES_LEFT);
            db.execSQL(DBConstants.CREATE_EXPENSELEFT_SQL);

            db.execSQL(DBConstants.DROP_EXPENSES_RIGHT);
            db.execSQL(DBConstants.CREATE_EXPENSESRIGHT_SQL);

            db.execSQL(DBConstants.DROP_INCOME_TYPE);
            db.execSQL(DBConstants.CREATE_INCOMETYPE_SQL);

            db.execSQL(DBConstants.DROP_INCOME);
            db.execSQL(DBConstants.CREATE_INCOME_SQL);

            db.execSQL(DBConstants.DROP_MEMBER);
            db.execSQL(DBConstants.CREATE_MEMBER_SQL);

            db.execSQL(DBConstants.DROP_REMIND);
            db.execSQL(DBConstants.CREATE_REMIND_SQL);

            db.execSQL(DBConstants.DROP_TIME_SEQUENCE);
            db.execSQL(DBConstants.CREATE_TIMESEQUENCE_SQL);

            db.execSQL(DBConstants.DROP_TRANSFER);
            db.execSQL(DBConstants.CREATE_TRANSFER_SQL);

        }
    }

    public void initDataBase() {
        if(db == null) {
            db = getWritableDatabase();
        }
    }

}
